package aiku.numericalcontroloa.project.system.service.impl;

import aiku.numericalcontroloa.common.annotation.ExceptionMessage;
import aiku.numericalcontroloa.common.annotation.StartPage;
import aiku.numericalcontroloa.project.system.mapper.SysMenuMapper;
import aiku.numericalcontroloa.project.system.mapper.SysRoleMapper;
import aiku.numericalcontroloa.project.system.pojo.entity.SysRole;
import aiku.numericalcontroloa.project.system.pojo.query.SysRoleParams;
import aiku.numericalcontroloa.project.system.service.SysRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

//    @Resource
//    private SysUserMapper sysUserMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    @ExceptionMessage("获取权限数据失败")
    public List<String> listRolePerms(String rid) {
        return sysRoleMapper.listRolePerms(rid);
    }

    @Override
    @ExceptionMessage("新增角色信息失败")
    public int insert(SysRole role) {
//        String user = RequestContext.getCurrentUserModel().getUsername();
        String user = "temp_admin";
        role.setCreateUser(user);
        role.setCreateTime(new Date());
        return sysRoleMapper.insert(role);
    }

    @Override
    @ExceptionMessage("获取角色列表数据失败")
    public List<SysRole> list() {
        return sysRoleMapper.list();
    }


    @Override
    @Transactional
    @ExceptionMessage("更新角色权限信息失败")
    public int handlePerms(SysRoleParams record) {
        String rid = record.getId();
        List<Integer> menusId = record.getMenusId();

        //若取消该角色所有权限，则只删除该角色对应的角色-权限表
        if (menusId.size() == 0) {
            sysRoleMapper.deleteRolePerms(rid);
            return 1;
        } else {
            //先删除角色对应的权限数据，再批量新增
            sysRoleMapper.deleteRolePerms(rid);
            int result = sysRoleMapper.handleRolePerms(rid, menusId);
            if (result == menusId.size()) {
                return result;
            }
        }
        return 0;
    }


    @Override
    @ExceptionMessage("获取角色数据失败")
    @StartPage
    public PageInfo<SysRole> pageList() {
        List<SysRole> roleLis = sysRoleMapper.list();
        return new PageInfo<>(roleLis);
    }

    @Override
    @Transactional
    @ExceptionMessage("删除角色失败")
    public int deleteRole(SysRoleParams query) {
//        //查询该角色是否存在用户
//        List<SysUser> userList = sysUserMapper.listByRoleId(query.getId());
//        if (userList.size() > 0) {
//            return -1;
//        }
//
//        //查询该角色是否有分配菜单
//        List<Integer> integers = sysRoleMapper.listRolePerms(query.getId());
//        if (integers.size() > 0 && integers.get(0) != null) {
//            return -2;
//        }
//
//        //若均符合上述条件，则删除该角色
//        return sysRoleMapper.deleteByPrimaryKey(query.getId());
        return  1;
    }


    @Override
    @ExceptionMessage("查找单个角色失败")
    public SysRole selectByPrimaryKey(Integer id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    @ExceptionMessage("修改角色失败")
    public int updateByPrimaryKey(SysRole record) {
//        SysUser user = RequestContext.getCurrentUserModel();
//        record.setModifyUser(user.getUsername());
        record.setModifyTime(new Date());
        return sysRoleMapper.updateByPrimaryKey(record);
    }

    @ExceptionMessage("批量添加菜单和功能失败")
    public void insertRoleMenuAll(SysRole record) {
//        record.setCreateUser(RequestContext.getCurrentUserModel().getUsername());
        record.setCreateTime(new Date());
        sysRoleMapper.insertRoleMenu(record);
    }

    @Override
    @ExceptionMessage("根据用户id查找角色失败")
    public List<SysRole> listByUserId(SysRoleParams query) {
        return sysRoleMapper.listByUserId(query.getId());
    }


    @Override
    @ExceptionMessage("查找角色状态失败")
    public int updateRoleStatus(SysRole role) {
        return sysRoleMapper.updateRoleStatus(role);
    }

    @Override
    @ExceptionMessage("查找已启动的角色失败")
    public List<SysRole> ListByEnabled() {
        return sysRoleMapper.ListByEnabled();
    }


}
